package com.love.marriage.userProfileInfo.controller;

import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.common.interfaces.avoid.AvoidRepeatableCommit;
import com.love.marriage.common.interfaces.avoid.LogRestRequest;
import com.love.marriage.common.validator.Insert;
import com.love.marriage.common.validator.Update;
import com.love.marriage.common.vo.Result;
import com.love.marriage.common.vo.ResultBoolean;
import com.love.marriage.userProfileInfo.service.UserProfileInfoService;
import com.love.marriage.userProfileInfo.vo.ResultUserProfileInfoVo;
import com.love.marriage.userProfileInfo.vo.ResultUserProfileInfoVoPage;
import com.love.marriage.userProfileInfo.vo.UserProfileInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpServletRequest;

/**
 * description:  用户档案表restApi
 * author:       mybatisPlus
 * createDate:   2024-10-12 21:01:09
 * version:      1.0.0
 */
@ApiSort(105)
@Tag(name = "用户档案表相关接口", description = "用户档案表相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/user-profile-info")
public class UserProfileInfoController {

    private final UserProfileInfoService userProfileInfoService;

    @ApiOperationSupport(order = 10, author = "alex")
    @Operation(summary = "获取用户档案表分页", description = "获取用户档案表分页")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserProfileInfoVoPage.class
                    )))
    @PostMapping(value = "/page")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    public Result<Page<UserProfileInfoVo>> getPage(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                                   @RequestParam(value = "pageSize", required = false) Long pageSize,
                                                   @RequestBody(required = false) UserProfileInfoVo userProfileInfoVo) {
        return Result.success(userProfileInfoService.getPage(pageNum, pageSize, userProfileInfoVo));
    }

    @ApiOperationSupport(order = 15, author = "alex")
    @Operation(summary = "获取用户档案表分页(带会员情况)", description = "获取用户档案表分页(带会员情况)")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserProfileInfoVoPage.class
                    )))
    @PostMapping(value = "/new-page")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    public Result<Page<UserProfileInfoVo>> getPageWithMembership(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                                                 @RequestParam(value = "pageSize", required = false) Long pageSize,
                                                                 @RequestBody(required = false) UserProfileInfoVo userProfileInfoVo) {
        return Result.success(userProfileInfoService.getPageWithMembership(pageNum, pageSize, userProfileInfoVo));
    }

    @ApiOperationSupport(order = 20, author = "alex")
    @Operation(summary = "获取用户档案表详情", description = "获取用户档案表详情")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserProfileInfoVo.class
                    )))
    @Parameters({
            @Parameter(name = "id", description = "id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @GetMapping
    public Result<UserProfileInfoVo> query(@RequestParam(value = "id") Long id) {
        return Result.success(userProfileInfoService.queryUserProfileInfo(id));
    }

    @ApiOperationSupport(order = 20, author = "alex")
    @Operation(summary = "获取用户档案表详情", description = "获取用户档案表详情")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserProfileInfoVo.class
                    )))
    @Parameters({
            @Parameter(name = "userId", description = "用户id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @Tag(name = "我的", description = "我的")
    @GetMapping("/queryUserProfileInfo")
    public Result<UserProfileInfoVo> queryByUserId(@RequestParam(value = "userId") Long userId) {
        return Result.success(userProfileInfoService.queryUserProfileInfoByUserId(userId));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 30, author = "alex")
    @Operation(summary = "新增用户档案表", description = "新增用户档案表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = true,
                    content = @Content(schema = @Schema(implementation = UserProfileInfoVo.class, requiredProperties = {"userId"}))
            ))
    @LogRestRequest(apiName = "新增用户档案表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserProfileInfoVo.class)))
    @PostMapping
    @Tag(name = "我的", description = "我的")
    public Result<UserProfileInfoVo> add(@Validated({Insert.class}) @RequestBody UserProfileInfoVo userProfileInfoVo) throws Exception {
        return Result.success(userProfileInfoService.addUserProfileInfo(userProfileInfoVo));
    }

    @ApiOperationSupport(order = 40, author = "alex")
    @Operation(summary = "修改用户档案表", description = "修改用户档案表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserProfileInfoVo.class))
            ))
    @LogRestRequest(apiName = "修改用户档案表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserProfileInfoVo.class)))
    @PutMapping
    @Tag(name = "我的", description = "我的")
    public Result<UserProfileInfoVo> update(@Validated({Update.class}) @RequestBody UserProfileInfoVo userProfileInfoVo) throws Exception {
        return Result.success(userProfileInfoService.updateUserProfileInfo(userProfileInfoVo));
    }

    @ApiOperationSupport(order = 50, author = "alex")
    @Operation(summary = "刪除用户档案表", description = "刪除用户档案表")
    @LogRestRequest(apiName = "刪除用户档案表")
    @Parameters({
            @Parameter(name = "ids", description = "ids,用，分割", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1,2"))
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.success(userProfileInfoService.deleteUserProfileInfo(ids));
    }

    @ApiOperationSupport(order = 90, author = "alex")
    @Operation(summary = "关联用户电话", description = "关联用户电话")
    @LogRestRequest(apiName = "关联用户电话")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class
                    )))
    @Parameters({
            @Parameter(name = "userId", description = "用户id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1")),
            @Parameter(name = "phone", description = "电话", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1822222")),
            @Parameter(name = "code", description = "验证码", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1822222"))
    })
    @Tag(name = "用户", description = "用户信息")
    @Tag(name = "我的", description = "我的")
    @GetMapping(value = "/concatPhone")
    public Result<String> concatPhone(HttpServletRequest request,
                                      @RequestParam(value = "userId") Long userId,
                                      @RequestParam(value = "phone") String phone,
                                      @RequestParam(value = "code") String code) throws Exception {
        return Result.success(userProfileInfoService.concatPhone(request, userId, phone, code, "changePhone"));
    }

    @ApiOperationSupport(order = 90, author = "alex")
    @Operation(summary = "通过编码关联用户电话", description = "通过编码关联用户电话")
    @LogRestRequest(apiName = "通过编码关联用户电话")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultBoolean.class
                    )))
    @Parameters({
            @Parameter(name = "userId", description = "用户id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1")),
            @Parameter(name = "code", description = "编码", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1822222"))
    })
    @Tag(name = "用户", description = "用户信息")
    @Tag(name = "我的", description = "我的")
    @GetMapping(value = "/concatPhoneByCode")
    public Result<String> concatPhoneByCode(HttpServletRequest request,
                                            @RequestParam(value = "userId") Long userId,
                                            @RequestParam(value = "code") String code) throws Exception {
        return Result.success(userProfileInfoService.concatPhoneByCode(request, userId, code));
    }

    @ApiOperationSupport(order = 100, author = "alex")
    @Operation(summary = "通过编码获取用户电话", description = "通过编码获取用户电话")
    @LogRestRequest(apiName = "通过编码获取用户电话")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class
                    )))
    @Parameters({
            @Parameter(name = "code", description = "编码", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1822222"))
    })
    @Tag(name = "用户", description = "用户信息")
    @Tag(name = "我的", description = "我的")
    @GetMapping(value = "/getPhoneByWechatCode")
    public Result<WxMaPhoneNumberInfo> getPhoneByWechatCode(HttpServletRequest request,
                                                            @RequestParam(value = "code") String code) throws Exception {
        return Result.success(userProfileInfoService.getPhoneByWechatCode(request, code));
    }

    @ApiOperationSupport(order = 110, author = "alex")
    @Operation(summary = "导出用户档案(含择偶要求)", description = "导出用户档案(含择偶要求)")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultBoolean.class
                    )))
    @PostMapping(value = "/export")
    public void export(@RequestBody(required = false) UserProfileInfoVo userProfileInfoVo, HttpServletResponse response) throws Exception {
        userProfileInfoService.export(userProfileInfoVo, response);
    }
}
